package com.liyuan.yiqixing.dao;

import com.liyuan.yiqixing.model.Discussion;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface DiscussionDao {
    @Select("select id,user,classification,title,left(content,30) as content,modifiedAt,readCount,likeCount from discussion where classification=#{classification} order by id desc limit #{scale} offset #{offset}")
    Discussion[] selectDiscussions(@Param("offset") int offset,
                                   @Param("scale") Integer scale,
                                   @Param("classification") String classification);

    @Insert("insert into discussion(user,title,content,modifiedAt,classification)values(#{user.id},#{title},#{content},#{modifiedAt},#{classification})")
    void insertDiscussion(Discussion discussion);

    @Select("select count(*) from discussion where classification = #{classification}")
    int sum(String classification);

    @Select("select * from discussion where id = #{id}")
    Discussion selectDiscussion(Integer id);

    @Select("select LAST_INSERT_ID()")
    int selectLastInsertId();

    @Select("select left(title,10) as title from discussion where id = #{targetId}")
    String selectContent(Integer targetId);
}
